#include "ftpClient.h"
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
    char* port;
    char* ip;
    char* user;

    char* password;
    password = malloc(sizeof(char)* 50);

    SOCKET* s;
    s = malloc(sizeof(s));
    SSL* sslSocket;

    bool enableSSL = false;

    ///////////////////////////////////////////////////////////

    if (argc < 4)
    {
        fprintf(stderr,  "ERR: Format d'exécution incorrect : ./ftpClient [IP] [PORT] [USER] [PASSWORD (optional)]\n");
        exit(1);
    }
    else
    {
        ip = argv[1];
        port = argv[2]; //Gérer quand c'est pas un int
        user = argv[3];

        if (argc >= 5)
        {
            password = argv[4];
        }
        else
        {
            printf("Quel est le mot de passe ?\n");
            SetStdinEcho(false);
            scanf("%49s", password);
            SetStdinEcho(true);
        }
    }

    fprintf(stderr, "OK: Lancement de la connection\n");

    *s = creeConnectSocketClient(ip, port);

    recoieMessage(1, &s, enableSSL);

    char* message;
    message = malloc(sizeof(char)* 256);

    recoieMessage(1, s, enableSSL);

    envoieMessage(s, enableSSL, "AUTH TLS\r\n");

    message = recoieMessage(1, s, enableSSL);

    if(chercheCode(message, "234"))
    {
        enableSSL = TRUE;
        fprintf(stderr, "OK: SSL disponible\n");
    }
    else
    {
        fprintf(stderr, "ERR: SSL non disponible\n");
    }

    free(message);

    if (enableSSL)
    {
        const SSL_METHOD* method = SSLv23_client_method();
        SSL_CTX* ctx = NULL;

        SSL_library_init();
        SSL_load_error_strings();

        ctx = SSL_CTX_new(method);

        if (ctx == NULL)
        {
            ERR_print_errors_fp(stderr);
        }
        SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);

        sslSocket = SSL_new(ctx);

        if (!sslSocket)
        {
            ERR_print_errors_fp(stderr);
            return 1;
        }
        if (!SSL_set_fd(sslSocket, *s))
        {
            ERR_print_errors_fp(stderr);
            return 1;
        }
        if (SSL_connect(sslSocket) < 1)
        {
            ERR_print_errors_fp(stderr);
            return 1;
        }

        envoieMessage(sslSocket, enableSSL, "USER %s\r\n", user);

        recoieMessage(1, sslSocket, enableSSL);

        envoieMessage(sslSocket, enableSSL, "PASS %s\r\n", password);

        recoieMessage(1, sslSocket, enableSSL);
    }
    else
    {
        envoieMessage(s, enableSSL, "USER %s\r\n", user);

        recoieMessage(1, s, enableSSL);

        envoieMessage(s, enableSSL, "PASS %s\r\n", password);

        recoieMessage(1, s, enableSSL);
    }

    if(enableSSL == true)
    {
        waitCmd(sslSocket, ip, enableSSL);
        if (SSL_shutdown((SSL*)sslSocket) < 1)
        {
            ERR_print_errors_fp(stderr);
        }
    }
    else
    {
        waitCmd(s, ip, enableSSL);
        close((SOCKET)*s);
    }

    /*free(&user);
    free(&password);
    free(&ip);
    free(&port);*/

    return 0;
}
